package org.jeecg.modules.jxc.purchase.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.modules.demo.jxc.baseData.entity.JxcSupplier;
import org.jeecg.modules.jxc.homepage.config.PageResult;
import org.jeecg.modules.jxc.homepage.entity.JxcGoods;
import org.jeecg.modules.jxc.homepage.mapper.JxcGoodsMapper;
import org.jeecg.modules.jxc.homepage.service.IJxcGoodsService;
import org.jeecg.modules.jxc.purchase.entity.*;
import org.jeecg.modules.jxc.purchase.mapper.PurchaseGoodsMapper;
import org.jeecg.modules.jxc.purchase.mapper.PurchaseMapper;
import org.jeecg.modules.jxc.purchase.mapper.ReturnGoodsMapper;
import org.jeecg.modules.jxc.purchase.mapper.ReturnPurchaseMapper;
import org.jeecg.modules.jxc.purchase.service.PurchaseService;
import org.jeecg.modules.jxc.purchase.util.TreeToolUtils;
import org.jeecg.modules.jxc.statistical.entity.JxcPurchaseList;
import org.jeecg.modules.jxc.statistical.mapper.JxcPurchaseListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @description:
 * @company: yaorange
 * @author: Mr.Huang
 * @version: 1.0
 * @create: 2021-12-21 15:52:08
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private org.jeecg.modules.demo.jxc.baseData.service.IJxcSupplierService  jxcSupplierService;
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private JxcGoodsMapper jxcGoodsMappe;
    @Autowired
    private JxcPurchaseListMapper purchaseListMapper;
    @Autowired
    private PurchaseGoodsMapper purchaseGoodsMapper;
    @Autowired
    private ReturnPurchaseMapper returnPurchaseMapper;
    @Autowired
    private ReturnGoodsMapper returnGoodsMapper;

    @Override
    public List<JxcSupplier> getList() {
        List<JxcSupplier> list = jxcSupplierService.list();
        return list;
    }
    @Override
    public List<GoodsType> getGoodsType() {

        List<GoodsType> list = purchaseMapper.selectList(new QueryWrapper<>());

        ArrayList<GoodsType> rootList = new ArrayList<>();
        ArrayList<GoodsType> bodyList = new ArrayList<>();
        for(GoodsType goodsType:list){
            if(goodsType.getPId()==-1){
                rootList.add(goodsType);
            }
            bodyList.add(goodsType);
        }
        TreeToolUtils utils = new TreeToolUtils(rootList, bodyList);
        List<GoodsType> result = utils.getTree();
        return result;
    }

    @Override
    public Result<JxcGoods> getGoods(Integer pageNo, Integer pageSize, Integer id) {
        QueryWrapper<JxcGoods> wrapper = new QueryWrapper<JxcGoods>().eq("goods_type_id", id);
        Page<JxcGoods> list = jxcGoodsMappe.selectPage(new Page<>(pageNo, pageSize), wrapper);
        Integer count = jxcGoodsMappe.selectCount(wrapper);
        Result<JxcGoods> result = new Result<>();
        result.setTotal(list.getTotal());
        result.setRecords(list.getRecords());
        return result;
    }

    @Override
    public JxcGoods getGoodById(Integer id) {

        JxcGoods goods = jxcGoodsMappe.selectOne(new QueryWrapper<JxcGoods>().eq("goods_code", id));
        return goods;
    }

    @Override
    @Transactional
    public void addOrder(String orderId, List<PurchaseGoods> list, JxcPurchaseList purchase) {

        purchase.setPurchaseNumber(orderId);
        //新增进货单
        purchaseListMapper.insert(purchase);
        for(PurchaseGoods purchaseGoods:list){
            //新增进货商品单
            purchaseGoodsMapper.insert(purchaseGoods);
            //获取商品id
            Integer goodsId = purchaseGoods.getGoodsId();
            //获取商品进价
            Float price = purchaseGoods.getPrice();
            //获取商品进货数量
            Integer goodNum = purchaseGoods.getGoodNum();
            JxcGoods jxcGoods = jxcGoodsMappe.selectOne(new QueryWrapper<JxcGoods>().eq("id", goodsId));
            //更新库存
            jxcGoods.setInventoryQuantity(jxcGoods.getInventoryQuantity()+goodNum);
            //更新上次进价
            jxcGoods.setLasjxcPurchasingPrice(jxcGoods.getPurchasingPrice());
            //更新进价
            jxcGoods.setPurchasingPrice(Double.valueOf(String.valueOf(price)));
            //更新商品信息
            jxcGoodsMappe.updateById(jxcGoods);
        }
    }

    @Override
    @Transactional
    public Boolean returnOrder(String orderId, List<ReturnGoods> list, ReturnPurchase purchase) {
        for(ReturnGoods returnGoods:list){
            //新增退货商品单
            returnGoodsMapper.insert(returnGoods);
            JxcGoods jxcGoods = jxcGoodsMappe.selectOne(new QueryWrapper<JxcGoods>().eq("id", returnGoods.getGoodsId()));
            if(jxcGoods.getInventoryQuantity()<returnGoods.getGoodNum()){
                return false;
            }
            //更新库存
            jxcGoods.setInventoryQuantity(jxcGoods.getInventoryQuantity()-returnGoods.getGoodNum());
            //更新商品信息
            jxcGoodsMappe.updateById(jxcGoods);
        }
        purchase.setReturnNumber(orderId);
        //新增退货单
        returnPurchaseMapper.insert(purchase);
        return true;
    }

    @Override
    public List<Map<String, Object>> findPurchaseList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {

        if (StringUtils.isEmpty(purchaseNumber)){
            purchaseNumber = null;
        }
        if (StringUtils.isEmpty(sTime)){
            sTime = null;
        }
        if (StringUtils.isEmpty(eTime)){
            eTime = null;
        }
        if (StringUtils.isEmpty(supplierId.toString())){
            supplierId = null;
        }
        if (StringUtils.isEmpty(state.toString())){
            state = null;
        }
        List<Map<String, Object>> list = purchaseListMapper.selectPurchaseList(purchaseNumber,supplierId,state,sTime,eTime);

        System.out.println(list);
        return list;
    }
}
